package com.wenda.admin.abnormal;

import javax.xml.soap.Text;
import java.sql.*;
import java.util.Calendar;

public class connection {


    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://127.0.0.1/flow_admin?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";

    static final String USER = "root";
    static final String PASS = "123456";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{

            Class.forName(JDBC_DRIVER);


            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);


            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, create_time, content ,to_ip ,to_port FROM tb_flow";
            ResultSet rs = stmt.executeQuery(sql);
            String id  ;
            Timestamp time;
            String text ;
            String to_ip ;
            String to_port;
            double size = 0;
            double count = 0;
            //double temp1 = 0;
            double H = 0;
            double x = 0;
            double x1 = 0;
            //double temp2 = 0;
            double temp1 = 0;
            double temp2 = 0;
            int flag = 1;
            long time1 = 0;
            //test1 test = new test1();
            double[] p= new double[65536];
            double[] p2= new double[65536];
            int j = 0;
            while (rs.next()) {
                for (int i = 1; i < 65536; i++) {
                    p[i] = 0;
                }
                System.out.println("==================================================");
                for (int i = 1; rs.next(); i++) {
                    id = rs.getString("id");
                    System.out.println(id);
                    time = rs.getTimestamp("create_time");
                    text = rs.getString("content");
                    to_ip = rs.getString("to_ip");
                    to_port = rs.getString("to_port");
                    int tPort;
                    if (to_port != null && to_ip.equals("192.168.31.224")) {
                        tPort = Integer.parseInt(to_port);
                        p[tPort] = text.getBytes("utf-8").length + p[tPort];
                    }
                    if (flag == 1) {
                        time1 = time.getTime() + (long) 60 * 1000;
                        flag = 0;
                    }
                    if (time.getTime() > time1) {
                        flag = 1;
                        break;
                    }
                }
                for (int i = 1; i < 65536; i++) {
                    x1 = x1 + p[i];
                }
                H = math1(set(p, x1));
                p2[++j] = H;
                p = new double[65536];
                x1 = 0;

            }
            double max = p2[1];
            double min = p2[1];
            for (int i = 0; i < p2.length; i++){
                if (p2[i] != 0) {
                    if (min > p2[i]){
                        min = p2[i];
                    }
                    if (max < p2[i]){
                        max = p2[i];
                    }
                    System.out.println("p2: " + i + " " + p2[i]);
                }
            }
            System.out.println("min: " + min);
            System.out.println("max: " + max);

            for (int i = 0; i < p2.length; i++){
                if (p2[i] != min && p2[i] != max && p2[i] != 0) {
                    x++;
                    size = size + p2[i];
                    temp2 = temp2 + p2[i];
                    temp1 = temp1 + Math.pow(p2[i], 2);
                }
            }
            System.out.println("x: " + x);
            temp1 = temp1 * 1 / x;
            temp2 = Math.pow(temp2, 2) * 1 / x * 1/ x;
            System.out.println("参数1：" + Math.sqrt(temp1 - temp2));
            System.out.println((size / x) - (3 * Math.sqrt(temp1 - temp2)));
            System.out.println(size / x + 3 * Math.sqrt(temp1 - temp2));
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){

            se.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{

            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }

    static double math1(double[] p) {
        int n = 65536;
        double H = 0;
        for (int i = 1; i < p.length; i++) {
            if (p[i] != 0) {
                H += -p[i] * (Math.log(p[i]) / Math.log(2));
            }
        }

        System.out.println(H);
        System.out.println("*****************************");
        return H;

    }
    static double[] set (double[]  p, double x) {
        double M = x;

        double[] p1 = new double[65536];
        double m1;
        for (int i = 1; i < p.length; i++) {
            m1 = p[i];
            p1[i] = m1 / M;
        }
        return p1;
    }
}

//  H 字节数出口特征熵